//
//  BRQueryViewController.m
//  carcareIOS
//
//  Created by wr on 15/8/11.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "BRQueryViewController.h"
#import "ViolationModel.h"
#import "CarCityNamesView.h"
#import "NetViolations.h"
#import "BreakRulesNormalCell.h"
#import "SelectCityViewController.h"

#define CELL_HEIGHT (55)
#define SECTION_HEIGHT (12.5)

@interface BRQueryViewController ()<UIActionSheetDelegate,SelectCityDelegate,RulesListVCDelegate,NoRulesVCDelegate>

@property (nonatomic , strong) ViolationMgr * cityMgr;

@property (nonatomic , strong) UIView *cellTwoBGView;

@property (nonatomic , strong) CarCityNamesView * carCityV;

//车牌号签名的城市 而不是所查询的城市
@property (nonatomic , strong) UILabel *cityL;

@property (nonatomic , strong) UITextField *carNOTF;

@property (nonatomic, strong) UITextField *engineNOTF;

@property (nonatomic ,strong) UITextField * classaNOTF;

@property (nonatomic , copy) NSString * queryIDStr;

@property (nonatomic , copy) NSString * carNoStr;

@property (nonatomic , strong) NSMutableArray * cellTitleArray;

@property (nonatomic , strong) UIButton * queryButton;

@property (nonatomic , strong) ViolationCarModel * queryCarModel;

//是否在将要显示页面的时候刷新数据
@property (nonatomic , assign) BOOL willReloadBool;

@property (nonatomic , strong) ViolationCityModel * selectCityModel;

@end

@implementation BRQueryViewController
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (instancetype)initWithQueryIDStr:(NSString *)queryIDStr withOtherMessageModel:(ViolationCarModel *)queryDataModel withDataManager:(ViolationMgr *)cityMgr;
{
    self = [super initWithInputType:TYPE_INPUT_TABLEVIEW];
    if (self) {
        self.queryIDStr = queryIDStr;
        self.queryCarModel = queryDataModel;
        self.cityMgr = cityMgr;
        
        self.carNoStr = queryDataModel.carno;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"违章查询";
    
    self.willReloadBool = NO;

    [UmengMgr event:ICZ_CheckCarTraffic_AddCar];

    if (![self.queryIDStr isEqualToString:@"0"]) {
        __weak __typeof(self) wself = self;
        [self setupNavRightImageName:@"deleteBreakRulesImage" action:^{
            if(![Check isVisiter]){
                UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle:@"这将删除车辆的车牌号、车架号、发动机号等信息，确认要删除吗?" delegate:wself cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"确认删除", nil];
                sheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
                [sheet showInView:wself.view];
            }
        }];
    }

    self.tableView.frame = CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64 - (__MAIN_HEIGHT == 480 ? 110:140) * __MAIN_RATIO_H_667);
    self.tableView.scrollEnabled = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorColor = [Theme lineColor];
    [self.view addSubview:self.tableView];
    
    CGFloat  chelunImageW = 52 * __MAIN_RATIO_H_667;
    UIImageView * chelunImage = [Factory imageViewWithFrame:CGRectMake(0, 0, chelunImageW, chelunImageW) image:ImageNamed(@"cheLunImage") onView:self.view];
    [chelunImage setCenter:CGPointMake(__MAIN_WIDTH / 2, __MAIN_HEIGHT_64 - chelunImageW / 2 - 90 * __MAIN_RATIO_H_667)];
    [Factory labelWithFrame:CGRectMake(10, chelunImage.center.y + chelunImageW / 2 + 17 * __MAIN_RATIO_H_667, __MAIN_WIDTH - 10 * 2, 22) font:[UIFont systemFontOfSize:13] text:@"Powered by 车轮查违章" textColor:COLOR_HEXSTRING(@"#9E9E9E") onView:self.view textAlignment:NSTextAlignmentCenter];
    
    //初始化城市
    [self loadCity];
    
    __weak __typeof(self) wself = self;
    self.carCityV = [[CarCityNamesView alloc] initWithFrame:CGRectMake(0, __MAIN_HEIGHT_64, __MAIN_WIDTH, 346)];
    self.carCityV.Choice = ^(NSString *name){
        wself.cityL.text = name;
        [wself clickOnBG];
    };
    [self.view addSubview:self.carCityV];
    self.carCityV.hidden = YES;
    
//    UIPanGestureRecognizer * panResignKeyBoard = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panResignKeyBoard)];
//    [self.tableView addGestureRecognizer:panResignKeyBoard];
    
    [self.tableView reloadData];
    
    if (!self.queryCarModel) {
        self.queryCarModel = [[ViolationCarModel alloc]init];
    }
}
- (void)loadCity
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(upDateCityData) name:VIOLATION_CITY_UPDATE object:nil];
    if (self.cityMgr.provinceModels.count > 0 ) {
        if (self.queryCarModel) {
            for (int index = 0; index < self.cityMgr.provinceModels.count; index ++) {
                ViolationCityModel * model = self.cityMgr.provinceModels[index];
                ViolationCityModel * cityModel;
                if (model.subcities.count > 0) {
                    for (int indexJ = 0; indexJ < model.subcities.count; indexJ ++) {
                        cityModel = [[self.cityMgr subCityModelsWithSubCitys:model.subcities]objectAtIndex:indexJ];
                        if ([self.queryCarModel.eclicksCity isEqualToString:cityModel.apikey]) {
                            self.selectCityModel = cityModel;
                            return;
                        }
                    }
                }else
                {
                    cityModel = model;
                    if ([self.queryCarModel.eclicksCity isEqualToString:cityModel.apikey]) {
                        self.selectCityModel = cityModel;
                        return;
                    }
                }
            }
            
        }else
        {
            ViolationCityModel * model = self.cityMgr.provinceModels[0];
            ViolationCityModel * cityModel;
            if (model.subcities.count > 0) {
                cityModel = [[self.cityMgr subCityModelsWithSubCitys:model.subcities]objectAtIndex:0];
                self.selectCityModel = cityModel;
            }else
            {
                cityModel = model;
                self.selectCityModel = cityModel;
            }

        }
        NSString * oldSelectCityStr = self.queryCarModel.city;
        if (![oldSelectCityStr isEqualToString:self.selectCityModel.name]) {
            [self.tableView reloadData];
        }
    }
}
#pragma  mark - 更新城市通知
- (void)upDateCityData
{
    ViolationCityModel * model = [self.cityMgr.provinceModels firstObject];
    ViolationCityModel * cityModel = (model.subcities.count > 0 ? [[self.cityMgr subCityModelsWithSubCitys:model.subcities] firstObject] : [self.cityMgr.provinceModels firstObject]);
    UILabel * cityLabel = (UILabel *)[self.tableView viewWithTag:100];
    NSString * oldSelectCityStr = cityLabel.text;

    if (self.selectCityModel) self.selectCityModel = cityModel;
    if (![oldSelectCityStr isEqualToString:self.selectCityModel.name]) {
        [self.tableView reloadData];
    }
}
//- (void)panResignKeyBoard
//{
//    [self.tableView endEditing:YES];
//}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        __weak __typeof(self) wself = self;
        [self.cityMgr removeCarInfoWithID:[self.queryIDStr integerValue] finish:^{
            [wself.navigationController popViewControllerAnimated:YES];
        }];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.selectCityModel) {

        self.cellTitleArray = [NSMutableArray arrayWithCapacity:0];
        self.cellTitleArray = [NSMutableArray arrayWithObjects:@[@"查询城市"],@[@"车牌号码"], nil];
        
        if (self.selectCityModel.paramsKeyContents) {
            for (int index = 0 ; index < self.selectCityModel.paramsKeyContents.count ; index ++) {
                NSString * keyStr = self.selectCityModel.paramsKeyContents[index];
                NSString * titleStr = [NSString stringWithFormat:@"%@" , self.cityMgr.paramKeys[keyStr][K_NAME]];
                //0为名称  1为key值  2 为key后面的内容
                NSArray * smallTitleArray = [NSArray arrayWithObjects:titleStr, keyStr , self.selectCityModel.params[keyStr], nil];
                [self.cellTitleArray addObject:smallTitleArray];
            }
        }
        [self.cellTitleArray addObject:@[@"是否订阅违章信息"]];
    }
    
    return self.cellTitleArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return (section == 0 ? SECTION_HEIGHT : 0);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CELL_HEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell;
    
    if (indexPath.section == 1) {
        static NSString * cellID = @"cellID";
        cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            
            [Factory imageViewWithCenter:CGPointMake(__MAIN_WIDTH - 25, CELL_HEIGHT/2) image:ImageNamed(@"cell_right") onView:cell.contentView];

            CGFloat gap = 4;
//            COLOR_HEXSTRING(@"F2F2F2")
            UIView * cellTwoBGView = [Factory viewWithFrame:CGRectMake(100, gap, 57, CELL_HEIGHT - 2 * gap) bgColor: [UIColor clearColor] onView:cell.contentView];
            cellTwoBGView.tag = 11;
            

            [Factory imageViewWithCenter:CGPointMake(cellTwoBGView.width - 10, cellTwoBGView.height/2) image:ImageNamed(@"cell_right") onView:cellTwoBGView].transform = CGAffineTransformMakeRotation(M_PI / 2);
            
            UILabel * cityLabel = [Factory labelWithFrame:CGRectMake(15, 0, cellTwoBGView.width - 30, cellTwoBGView.height) font:FONT_DEFAULT_Light(16) text:@"" textColor:[Theme wordColor] onView:cellTwoBGView textAlignment:NSTextAlignmentLeft];
            cityLabel.tag = 12;
            
            UIButton * carNoButton = [Factory buttonEmptyWithFrame:cellTwoBGView.bounds click:nil onView:cellTwoBGView];
            carNoButton.tag = 13;
            
            CGFloat viewX = cellTwoBGView.frame.size.width + cellTwoBGView.frame.origin.x;
            CGFloat viewW = __MAIN_WIDTH - viewX - 25;
            UIView *bg2 = [Factory viewWithFrame:CGRectMake(viewX, gap, viewW, cellTwoBGView.height) bgColor:[UIColor clearColor] onView:cell];
            bg2.tag = 14;
            
            UITextField * carNoTF = [Factory textFieldWithFrame:bg2.bounds hideText:NO defaultText:@"请输入车牌号" onView:bg2];
            
            carNoTF.left = 10;
            carNoTF.width -= 20;
            carNoTF.tag = 15;
            
            [Factory viewWithFrame:CGRectMake(0, CELL_HEIGHT - 0.5, __MAIN_WIDTH, 0.5) bgColor:[Theme lineColor] onView:cell];
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = self.cellTitleArray[indexPath.section][indexPath.row];
        cell.textLabel.font = FONT_DEFAULT_Light(16);
        cell.textLabel.textColor = [Theme wordGrayColor];

        NSString *strCity = ([self.carNoStr isNotNil] ? [self.carNoStr substringToIndex:1] : @"沪");
        NSString *strNo = ([self.carNoStr isNotNil] ? [self.carNoStr substringFromIndex:1] : @"");
        self.cellTwoBGView = (UIView *)[cell viewWithTag:11];
        self.cellTwoBGView.layer.cornerRadius = 5;
        self.cellTwoBGView.layer.masksToBounds = YES;
        
        
        UILabel * cityLable = (UILabel *)[self.cellTwoBGView viewWithTag:12];
        self.cityL = cityLable;
        self.cityL.text = ([self.cityL.text isNotNil] ? self.cityL.text : strCity);
        
        UIButton * carNoButton = (UIButton *)[self.cellTwoBGView viewWithTag:13];
        [carNoButton addTarget:self action:@selector(carNoButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        self.carCityV.hidden = YES;

        UIView * bg2 = (UIView *)[cell viewWithTag:14];
        
        self.carNOTF = (UITextField *)[bg2 viewWithTag:15];
        self.carNOTF.text = strNo;
        self.carNOTF.textAlignment = NSTextAlignmentRight;
        self.carNOTF.keyboardType = UIKeyboardTypeASCIICapable;
        self.carNOTF.backgroundColor = [UIColor clearColor];
        self.carNOTF.textColor = [Theme wordColor];
        self.carNOTF.font = FONT_DEFAULT_Light(14);
        self.carNOTF.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
        [self registerTextField:self.carNOTF];

    }
    else
    {
        //switch的cell
        if (indexPath.section == [self.cellTitleArray count] - 1) {
            static NSString * cellID = @"isAutoID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                CGFloat switchViewX = 22 * __MAIN_RATIO_375;
                CGFloat switchViewW = __MAIN_WIDTH - switchViewX * 2;
                CGFloat switchViewH = CELL_HEIGHT;
                CGFloat switchViewY = 0;
                UIView * switchView = [Factory viewWithFrame:CGRectMake(switchViewX, switchViewY, switchViewW, switchViewH) bgColor:[UIColor clearColor] onView:cell];
                switchView.tag = 113;
                CGFloat switchW = 50;
                CGFloat switchH = 31;
                CGFloat switchY = (CELL_HEIGHT - switchH) / 2;
                CGFloat switchLabelW = 200;
                UILabel * switchLabel = [Factory labelWithFrame:CGRectMake(0, 0, switchLabelW, switchViewH) font:FONT_DEFAULT_Light(14) text:@"" textColor:[Theme wordGrayColor] onView:switchView textAlignment:NSTextAlignmentLeft];
                switchLabel.tag = 115;
                UISwitch * isAutoSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(switchViewW - switchW, switchY, switchW, switchH)];
                isAutoSwitch.on = self.queryCarModel.isAuto;
                isAutoSwitch.onTintColor = [Theme mainColor];
                isAutoSwitch.tag = 114;
                [switchView addSubview:isAutoSwitch];
                
                [Factory viewWithFrame:CGRectMake(0, CELL_HEIGHT - 0.5, __MAIN_WIDTH, 0.5) bgColor:[Theme lineColor] onView:cell];

            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            UIView * switchView = (UIView *)[cell viewWithTag:113];
            UISwitch * isAutoSwitch = (UISwitch *)[switchView viewWithTag:114];
            [isAutoSwitch addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventTouchUpInside];
            UILabel * switchLabel = (UILabel *)[switchView viewWithTag:115];
            switchLabel.text = self.cellTitleArray[indexPath.section][0];
        }else{
            static NSString * cellID = @"cellNomalID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[BreakRulesNormalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                [Factory viewWithFrame:CGRectMake(0, CELL_HEIGHT - 0.5, __MAIN_WIDTH, 0.5) bgColor:[Theme lineColor] onView:cell];
            }
            NSString * titleStr = self.selectCityModel.name;
            BOOL labelBool = (indexPath.section == 0 ? NO : YES);
            BOOL textBool = !labelBool;
            BOOL imageBool = NO;
            NSString * placeHoldStr;
            if (indexPath.section > 1 && ((self.cellTitleArray.count - 1) > 2) ) {
                
                placeHoldStr = ([self.cellTitleArray[indexPath.section][2] integerValue] == 0 ? [NSString stringWithFormat:@"请输入完整%@",self.cellTitleArray[indexPath.section][0]] : ([self.cellTitleArray[indexPath.section][2] integerValue] > 0 ? [NSString stringWithFormat:@"请输入%@前%@位",self.cellTitleArray[indexPath.section][indexPath.row],self.cellTitleArray[indexPath.section][2]] : [NSString stringWithFormat:@"请输入%@后%@位",self.cellTitleArray[indexPath.section][indexPath.row],[[NSString stringWithFormat:@"%@",self.cellTitleArray[indexPath.section][2]] substringFromIndex:1]]));
                
                if (([self.cellTitleArray[indexPath.section][1] rangeOfString:@"uname"].location != NSNotFound) || ([self.cellTitleArray[indexPath.section][1] rangeOfString:@"pwd"].location != NSNotFound )) {
                    imageBool = YES;
                }
            }
            [((BreakRulesNormalCell *)cell) configWithTextStr:self.cellTitleArray[indexPath.section][indexPath.row] labelHidden:labelBool labelStr:titleStr textFielDHidden:textBool textFildPlaceHold:placeHoldStr withIndex:indexPath.section rightImageHidden:imageBool];
            ((BreakRulesNormalCell *)cell).whyImageVButton.tag = indexPath.section;
            [((BreakRulesNormalCell *)cell).whyImageVButton addTarget:self action:@selector(whyImageVButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            
            if (self.queryCarModel && indexPath.section > 1) {
                NSArray * queryKeyArr = [self.queryCarModel.modelDic allKeys];
                for (int index = 0 ; index < queryKeyArr.count; index ++) {
                    NSString * queryKeyStr = queryKeyArr[index];
                    if ([queryKeyStr isEqualToString:self.cellTitleArray[indexPath.section][1]]) {
                        UITextField * inputTextfield = (UITextField *)[cell viewWithTag:200 + indexPath.section];
                        inputTextfield.text = self.queryCarModel.modelDic[queryKeyStr];
                    }
                }
            }
            if ([self.cellTitleArray[indexPath.section][0] isEqualToString:@"发动机号"])
            {
                self.engineNOTF = (UITextField *)[cell viewWithTag:200 + indexPath.section];
                [self registerTextField:self.engineNOTF];
            }else if ([self.cellTitleArray[indexPath.section][0] isEqualToString:@"车架号"])
            {
                self.classaNOTF = (UITextField *)[cell viewWithTag:200 + indexPath.section];
                [self registerTextField:self.classaNOTF];
            }
            if (indexPath.section > 1) {
                if ([self.cellTitleArray[indexPath.section][1] rangeOfString:@"pwd"].location != NSNotFound)
                {
                    UITextField * inputTextfield = (UITextField *)[cell viewWithTag:200 + indexPath.section];
                    inputTextfield.secureTextEntry = YES;
                }
            }

        }
        
    }

    return cell;
}
- (void)carNoButtonClick
{
    [self.tableView endEditing:YES];
    __weak __typeof(self) wself = self;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        wself.carCityV.hidden = NO;
        wself.carCityV.bottom = __MAIN_HEIGHT_64;
    } completion:nil];

}
#pragma mark - 用户名 密码的 问号按钮
- (void)whyImageVButtonClick:(UIButton *)sender
{
    NSInteger numIndex = sender.tag;
    NSString * keyStr = self.cellTitleArray[numIndex][1];
    NSString * urlStr = [NSString stringWithFormat:@"%@" , self.cityMgr.paramKeys[keyStr][K_LINK]];
    NSString * urlNameStr = [NSString stringWithFormat:@"%@" , self.cityMgr.paramKeys[keyStr][K_NAME]];
    NSString * urlDetailStr = [NSString stringWithFormat:@"%@" , self.cityMgr.paramKeys[keyStr][K_HINT]];
    __weak __typeof(self) wself = self;
    TipsView *tipV = [TipsView textTipsWithTitle:@"提示" lightTitles:@[urlDetailStr]];
    [tipV addLeftButton:@"取消" clickLeft:^{
        [AlertMgr dismiss:TYPE_VIEW_ALERT];
    } rightButton:@"前往" clickRight:^{
        BaseWebViewController * webVC = [[[BaseWebViewController alloc]initWithURL:urlStr]setupNavBack];
        webVC.title = urlNameStr;
        webVC.isScaleBool = YES;
        [wself.navigationController pushViewController:webVC animated:YES];
        [AlertMgr dismiss:TYPE_VIEW_ALERT];
    }];
    [AlertMgr showView:tipV];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView endEditing:YES];
    switch (indexPath.section) {
        case 0:
        {
            SelectCityViewController * selectVC = [[SelectCityViewController alloc]initWithData:self.cityMgr.provinceModels];
            selectVC.delegate = self;
            [self.navigationController pushViewController:[selectVC setupNavBack] animated:YES];
        }
            break;
            
        default:
            break;
    }
    
}
#pragma mark - selectCity delegate
- (void) configWithProvinceIndex:(NSInteger)provinceIndex withCityIndex:(NSInteger)cityIndex
{
    ViolationCityModel * model = [self.cityMgr.provinceModels objectAtIndex:provinceIndex];
    self.selectCityModel = (model.subcities.count > 0 ? [[self.cityMgr subCityModelsWithSubCitys:model.subcities]objectAtIndex:cityIndex] : [self.cityMgr.provinceModels objectAtIndex:provinceIndex]);
    UILabel * cityLabel = (UILabel *)[self.tableView viewWithTag:100];
//    self.selectCityStr = self.selectCityModel.name;
    if (![self.selectCityModel.name isEqualToString:cityLabel.text]) {
        self.willReloadBool = YES;
    }
    cityLabel.text = self.selectCityModel.name;
//    selectIndexPath = [NSIndexPath indexPathForRow:provinceIndex inSection:cityIndex];
    self.queryCarModel.ecode = @"";
    self.queryCarModel.vcode = @"";
    [self.tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return (section == self.cellTitleArray.count - 1 ? 100 : 0);
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * footView = [Factory viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, 100) bgColor:[Theme backGroundColor] onView:tableView.tableFooterView];
    
    
    CGFloat queryButtonX = 22 * __MAIN_RATIO_375;
    CGFloat queryButtonY = 17;
    
    __weak __typeof(self) wself = self;
    if (self.queryButton) {
        self.queryButton = nil;
        [self.queryButton removeFromSuperview];
    }
    self.queryButton = [ViewFactory buttonWithFrame:CGRectMake(queryButtonX, queryButtonY, __MAIN_WIDTH - queryButtonX * 2, 50 * __MAIN_RATIO_H_667) title:@"查询" normalColor:[Theme mainColor] highlightColor:[[Theme mainColor] colorWithAlphaComponent:0.5] disabledColor:[UIColor clearColor]  inView:footView action:^{
        if(![Check isVisiter]){
            [wself queryButtonClick];
        }
    }];

    return footView;
}
#pragma mark - switch
- (void)switchClick : (UISwitch *)switchSender
{
    BOOL isSwitchOn = [switchSender isOn];
    self.queryCarModel.isAuto = isSwitchOn;
}
#pragma  mark - 查询按钮
- (void)queryButtonClick
{
    [self.tableView endEditing:YES];
    
    [self.queryButton setEnabled:YES];
    
    if (self.cellTitleArray.count - 1 < 2) {
        [self.queryButton setEnabled:NO];
        return;
    }

    self.queryCarModel.eclicksCity = self.selectCityModel.apikey ;
    self.queryCarModel.cid = [self.queryIDStr integerValue];
    self.queryCarModel.city = self.selectCityModel.name;
    self.queryCarModel.carno = [NSString stringWithFormat:@"%@%@",self.cityL.text,self.carNOTF.text];
    
//    NSMutableDictionary * dataDic = [NSMutableDictionary dictionaryWithDictionary:@{@"isAuto" : @(self.queryCarModel.isAuto), @"eclicksCity" : self.selectCityModel.apikey , @"cid" : @([self.queryIDStr integerValue]), @"city" : self.selectCityModel.name , @"carno" : [NSString stringWithFormat:@"%@%@",self.cityL.text,self.carNOTF.text]}];
    
    NSString * reminderStr;
    if (self.carNOTF.text.length != 6) {
        reminderStr = @"请输入6位车牌号";
        [AlertMgr showShortTextTips:@[reminderStr]];
        return;
    }
    for (int index = 0; index < self.cellTitleArray.count - 1; index ++) {
        if (index > 1) {
            UITextField * inputTextField = (UITextField *)[self.tableView viewWithTag:200 + index];
            NSInteger textNum = [self.cellTitleArray[index][2] integerValue];
            NSInteger textNewNum = ([self.cellTitleArray[index][2] integerValue] < 0 ? [[[NSString stringWithFormat:@"%@",self.cellTitleArray[index][2]] substringFromIndex:1] integerValue]:[self.cellTitleArray[index][2] integerValue]);
            if ((inputTextField.text.length < textNewNum && textNewNum != 0) || (![inputTextField.text isNotNil])) {
                reminderStr = (textNum == 0 ? [NSString stringWithFormat:@"请输入完整%@",self.cellTitleArray[index][0]] : (textNum > 0 ? [NSString stringWithFormat:@"请输入%@前%@位",self.cellTitleArray[index][0],self.cellTitleArray[index][2]] : [NSString stringWithFormat:@"请输入%@后%@位",self.cellTitleArray[index][0],[[NSString stringWithFormat:@"%@",self.cellTitleArray[index][2]] substringFromIndex:1]]));
            }
            
            if ([reminderStr isNotNil]) {
                [AlertMgr showShortTextTips:@[reminderStr]];
                return;
            }else{
                NSString * keyStr = (([self.cellTitleArray[index][1] rangeOfString:@"uname"].location != NSNotFound ) ?  @"uname": (([self.cellTitleArray[index][1] rangeOfString:@"pwd"].location != NSNotFound ) ? @"pwd" : self.cellTitleArray[index][1]));
//                [dataDic setObject:inputTextField.text forKey:keyStr];
                [self.queryCarModel setValue:inputTextField.text forKey:keyStr];
            }
        }
    }
    
    __weak __typeof(self) wself = self;
    [self.cityMgr checkViolationWithCarInfo:self.queryCarModel.modelDic finish:^(NSArray *violationModels) {
        //如果是空 则进入  无违章记录页面
        if ([violationModels isKindOfClass:[NSNull class]] || violationModels.count == 0) {
//            wself.queryCarModel = [[ViolationCarModel alloc]initWithDic:dataDic];
            NoRulesViewController * noRulesVC = [[NoRulesViewController alloc]initWithQueryID:wself.queryIDStr];
            noRulesVC.delegate = wself;
            [wself.navigationController pushViewController:[noRulesVC setupNavBack] animated:YES];
        }else
        {
//            wself.queryCarModel = [[ViolationCarModel alloc]initWithDic:dataDic];
            RulesListViewController * listVC = [[RulesListViewController alloc]initWithData:violationModels initWithQueryID:wself.queryIDStr withDataManager:wself.cityMgr withCarModel:wself.queryCarModel];
            listVC.delegate = wself;
            [wself.navigationController pushViewController:[listVC setupNavBack] animated:YES];
        }
    }];
    
}
#pragma mark - noRules delegate
- (void) configQueryIDStr:(NSString *)queryIDStr
{
    self.queryIDStr = queryIDStr;
    
    self.carNoStr = ([self.carNoStr isNotNil] ? (([self.cityL.text isNotNil] && [self.carNOTF.text isNotNil]) ? [NSString stringWithFormat:@"%@%@",self.cityL.text,self.carNOTF.text] : @"") : (([self.cityL.text isNotNil] && [self.carNOTF.text isNotNil]) ? [NSString stringWithFormat:@"%@%@",self.cityL.text,self.carNOTF.text] : @""));
    
    
}
- (void)clickOnBG{
    [super clickOnBG];
    __weak __typeof(self) wself = self;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        wself.carCityV.top = __MAIN_HEIGHT_64;
    } completion:nil];
}
- (void)keyBoardWillShow{
    __weak __typeof(self) wself = self;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        wself.carCityV.top = __MAIN_HEIGHT_64;
    } completion:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [self.delegate configPageReloadData];
    [self.carNOTF resignFirstResponder];
    [self.tableView endEditing:YES];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    __weak __typeof(self) wself = self;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        wself.carCityV.bottom = __MAIN_HEIGHT_64;
    } completion:nil];
    
}
-(BOOL)textFieldCanAddString:(NSString *)string withTextField:(UITextField *)textField
{
    if (textField == self.carNOTF) {
        if (![Check isNumOrBigASCII:string]) {
            return NO;
        }
    }
    if (textField == self.engineNOTF && self.engineNOTF) {
        if (![Check isNumOrBigASCIIEngionNO:string]) {
            return NO;
        }
    }
    if (textField == self.classaNOTF && self.classaNOTF) {
        if (![Check isNumOrBigASCIICarFrame:string]) {
            return NO;
        }
    }
    return YES;
}
- (void)textFieldTextChanged:(UITextField *)textField{
    if (textField == self.carNOTF || textField == self.engineNOTF || textField == self.classaNOTF) {
        textField.text = [textField.text uppercaseString];
    }
}
- (NSInteger)textFieldMaxNumberWithTextField:(UITextField *)textField
{
    if (textField == self.carNOTF)
    {
        return 5;
    }
    else if(textField == self.engineNOTF)
    {
        return 20;
    }
    else if(textField == self.classaNOTF)
    {
        return 20;
    }
    else
    {
        return [super textFieldMaxNumberWithTextField:textField];
    }
}
- (void)viewWillAppear:(BOOL)animated
{

    if (self.willReloadBool) {
        [self.tableView reloadData];
    }
    self.carNoStr = ([self.carNoStr isNotNil] ? ([self.carNOTF.text isNotNil] ? [NSString stringWithFormat:@"%@%@",self.cityL.text,self.carNOTF.text]:self.carNoStr ): (([self.cityL.text isNotNil] && [self.carNOTF.text isNotNil]) ? [NSString stringWithFormat:@"%@%@",self.cityL.text,self.carNOTF.text] : @""));
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
